package com.intelligentmeterreadingsys.waterlist.reviewdata.activity;

import java.util.ArrayList;
import java.util.List;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.UserWaterVO;
import com.intelligentmeterreadingsys.util.Model;
import com.intelligentmeterreadingsys.waterlist.db.WaterListDaoImpl;
import com.intelligentmeterreadingsys.waterlist.querywaterlist.activity.ListViewAdapter;
import com.intelligentmeterreadingsys.waterlist.querywaterlist.activity.QueryWaterListActivity;
import com.intelligentmeterreadingsys.waterlist.reviewdata.db.ReviewDataDao;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 复查数据列表信息
 * @author king
 *
 */
public class ReviewListActivity extends Activity implements OnScrollListener{
	 List<String> items = new ArrayList<String>();  
	    private ListView listView;  
	    private int visibleLastIndex = 0;   //最后的可视项索引  
	    private int visibleItemCount;       // 当前窗口可见项总数  
	    private ListViewAdapter adapter;  
	    private View loadMoreView;  
	    private Button loadMoreButton;  
	    private Handler handler = new Handler();  
	    private int count=0;//总页数
	    public ReviewDataMainActivity act=null;//父类
	    private List<UserWaterVO> list=null;
	    private int page=1;//当前页数
	    private TextView textview;//页数信息
	    private EditText edit;//输入页数
	    private String waterid;
	    public Button button2;//上一页面;
	    public Button button3;//下一页
	    private Model model;
	    public List<UserWaterVO> getList() {
			return list;
		}

		public void setList(List<UserWaterVO> list) {
			this.list = list;
		}

		public int getPage() {
			return page;
		}

		public void setPage(int page) {
			this.page = page;
		}

		public String getWaterid() {
			return waterid;
		}

		public void setWaterid(String waterid) {
			this.waterid = waterid;
		}

		public ListView getListView() {
			return listView;
		}

		public void setListView(ListView listView) {
			this.listView = listView;
		}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		  setContentView(R.layout.listview);  
	        
	               
	        loadMoreView = getLayoutInflater().inflate(R.layout.listview_button, null);  
	        loadMoreButton = (Button) loadMoreView.findViewById(R.id.listview_ok);
	        button2=(Button)loadMoreView.findViewById(R.id.listview_lastpage);
	        button3=(Button)loadMoreView.findViewById(R.id.listview_nextpage);
            button3.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {


					// TODO Auto-generated method stub
					button3.setText("正在加载...");   //设置按钮文字loading  
					page++;					
					if(page>=count){
						page=count;
						button3.setText("下一页"); 
						Toast.makeText(ReviewListActivity.this, "已经是最后一页了！！！", 1000).show();
						return;
					}
			        handler.postDelayed(new Runnable() {  
			            @Override  
			            public void run() {  			                
			            flip();			                
			               //visibleLastIndex - visibleItemCount + 1
			                button3.setText("下一页");    //恢复按钮文字  
			            }  
			        }, 1000);  
				
					
				
					
				}
			});
	        button2.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {

					// TODO Auto-generated method stub
					button2.setText("正在加载...");   //设置按钮文字loading  
					page--;					
					if(page<=0){
						page=1;
						 button2.setText("上一页");
						 Toast.makeText(ReviewListActivity.this, "已经是第一页了！！！", 1000).show();
						return;
					}
			        handler.postDelayed(new Runnable() {  
			            @Override  
			            public void run() {  			                
			            flip();	                
			               //visibleLastIndex - visibleItemCount + 1
			                button2.setText("上一页");    //恢复按钮文字  
			            }  
			        }, 1000);  
				
					
				}
			});
	        textview=(TextView)loadMoreView.findViewById(R.id.listview_info);
	        edit=(EditText)loadMoreView.findViewById(R.id.listview_pagenumber);
	     
           loadMoreButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					loadMoreButton.setText("正在加载...");   //设置按钮文字loading  
					String s=edit.getEditableText().toString();
					if(TextUtils.isEmpty(s)){
						loadMoreButton.setText("确定");
						return;
					}
					page=Integer.parseInt(s);
					if(page<=0){
						Toast.makeText(ReviewListActivity.this, "页数不能小于1！！！", 1000).show();
						loadMoreButton.setText("确定");
						return;
					}
			        handler.postDelayed(new Runnable() {  
			            @Override  
			            public void run() {  			                
			             flip();			                
			               //visibleLastIndex - visibleItemCount + 1
			                loadMoreButton.setText("确定");    //恢复按钮文字  
			            }  
			        }, 1000);  
				}
			});
	        listView = (ListView) this.findViewById(R.id.listView1);
	          
	        listView.addFooterView(loadMoreView);   //设置列表底部视图  
	       // listView.addHeaderView(v)    //设置列表顶部视图
	        adapter = new ListViewAdapter(items,this);  
	        listView.setAdapter(adapter);                //自动为id是list的ListView设置适配器  
	        
	        listView.setOnScrollListener(this);     //添加滑动监听 
	  
	      //  initAdapter();  	        
	        listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int position, long arg3) {
				 
				}
			});
	        
	        queryDater();
	}

	   /** 
     * 模拟加载数据 
     */  
    private void loadData() {  
    	
    	
    	if(page>=count){
    		Toast.makeText(ReviewListActivity.this, "已到最后一页了", 1000).show();
    		page=count;
    			
    	}
        if(count==0){	    		
    		adapter.removeItem();	
    		adapter.notifyDataSetChanged(); //数据集变化后,通知adapter  
    		items.add("没有数据..........");	
    		 listView.setSelection(0); //设置选中项  
             textview.setText("页数"+page+"/"+count+"   跳转到");
    	}else{
    		adapter.removeItem();	
    		adapter.notifyDataSetChanged(); //数据集变化后,通知adapter  
             
            listView.setSelection(0); //设置选中项  
            textview.setText("页数"+page+"/"+count+"   跳转到");
	  	     
	    	if(count==0){
	    		Toast.makeText(ReviewListActivity.this, "没有查到数据", 1000).show();
	    		items.add("没有数据...............");
	    		return ;
	    	}
	    	
	        for (int i = 0; i < list.size(); i++) {  
	        	
	        	UserWaterVO use=list.get(i);
	        	
	            items.add(use.getName()+"  "+use.getWateradds()+"("+use.getCopystatus()+")");  
	        }  	       
    	}
    	
    }

    /** 
     * 滑动时被调用 
     */  
    @Override  
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {  
        this.visibleItemCount = visibleItemCount;  
        visibleLastIndex = firstVisibleItem + visibleItemCount - 1;  
    }  
  
    /** 
     * 滑动状态改变时被调用 
     */  
    @Override  
    public void onScrollStateChanged(AbsListView view, int scrollState) {  
        int itemsLastIndex = adapter.getCount() - 1;    //数据集最后一项的索引  
        int lastIndex = itemsLastIndex + 1;             //加上底部的loadMoreView项  
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == lastIndex) {  
            //如果是自动加载,可以在这里放置异步加载数据的代码  
            Log.i("LOADMORE", "loading...");  
        }  
    }  
    /**
     * 查询数据
     */
	public void queryDater(){
		 count=0;
		 page=1;
		 act=(ReviewDataMainActivity)getParent();		       
			ReviewDataDao daos=new ReviewDataDao(this);
			model=new Model(page, 10);
			list=daos.queryWaterlist(model, act.getUsernumber(), act.getWaterproperties(),
					act.getWateradds(), act.getCopystatus());
			count=daos.queryTotalPage(act.getUsernumber(), act.getWaterproperties(),
					act.getWateradds(), act.getCopystatus());
			
			daos.close();
			act.setList(list);
		     loadData();
	}
	
	/**
	 * 翻页
	 */
	public void flip(){
		act=(ReviewDataMainActivity)getParent();		       
		ReviewDataDao dao=new ReviewDataDao(this);
		System.out.println(page);
		model=new Model(page, 10);
		list=dao.queryWaterlist(model, act.getUsernumber(), act.getWaterproperties(),
				act.getWateradds(), act.getCopystatus());
		act.setList(list);
		dao.close();
	     loadData();
	}
 
}
